package com.ruoyi.basebill.service.impl;

import java.util.HashSet;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsSaleissuebillentry;
import com.ruoyi.basebill.mapper.OmsSaleissuebillMapper;
import com.ruoyi.basebill.domain.OmsSaleissuebill;
import com.ruoyi.basebill.service.IOmsSaleissuebillService;

/**
 * 销售出库单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-15
 */
@Service
public class OmsSaleissuebillServiceImpl implements IOmsSaleissuebillService 
{
    @Autowired
    private OmsSaleissuebillMapper omsSaleissuebillMapper;

    @Autowired
    private OmsSaleissuebillServiceImpl saleissuebillService;

    /**
     * 查询销售出库单
     * 
     * @param id 销售出库单主键
     * @return 销售出库单
     */
    @Override
    public OmsSaleissuebill selectOmsSaleissuebillById(Long id)
    {
        return omsSaleissuebillMapper.selectOmsSaleissuebillById(id);
    }

    @Override
    public List<OmsSaleissuebill> selectOmsSaleissuebillByIds(HashSet<Long> ids)
    {
        return omsSaleissuebillMapper.selectOmsSaleissuebillByIds(ids);
    }

    /**
     * 查询销售出库单列表
     * 
     * @param omsSaleissuebill 销售出库单
     * @return 销售出库单
     */
    @Override
    public List<OmsSaleissuebill> selectOmsSaleissuebillList(OmsSaleissuebill omsSaleissuebill)
    {
        return omsSaleissuebillMapper.selectOmsSaleissuebillList(omsSaleissuebill);
    }

    @Override
    public List<OmsSaleissuebill> selectOmsSaleissuebillDetailList(OmsSaleissuebill omsSaleissuebill) {
        return omsSaleissuebillMapper.selectOmsSaleissuebillDetailList(omsSaleissuebill);
    }

    /**
     * 新增销售出库单
     * 
     * @param omsSaleissuebill 销售出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSaleissuebill(OmsSaleissuebill omsSaleissuebill)
    {
        int rows = omsSaleissuebillMapper.insertOmsSaleissuebill(omsSaleissuebill);
        saleissuebillService.insertOmsSaleissuebillentry(omsSaleissuebill);
        return rows;
    }

    /**
     * 修改销售出库单
     * 
     * @param omsSaleissuebill 销售出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSaleissuebill(OmsSaleissuebill omsSaleissuebill)
    {
        //传入销售出库单不存在分录则不用修改分录信息只修改表头信息
        if(omsSaleissuebill.getOmsSaleissuebillentryList() != null && omsSaleissuebill.getOmsSaleissuebillentryList().size() != 0){
            omsSaleissuebillMapper.deleteOmsSaleissuebillentryByParentid(omsSaleissuebill.getId());
            saleissuebillService.insertOmsSaleissuebillentry(omsSaleissuebill);
        }
        return omsSaleissuebillMapper.updateOmsSaleissuebill(omsSaleissuebill);
    }

    /**
     * 批量删除销售出库单
     * 
     * @param ids 需要删除的销售出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSaleissuebillByIds(Long[] ids)
    {
        omsSaleissuebillMapper.deleteOmsSaleissuebillentryByParentids(ids);
        return omsSaleissuebillMapper.deleteOmsSaleissuebillByIds(ids);
    }

    /**
     * 删除销售出库单信息
     * 
     * @param id 销售出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSaleissuebillById(Long id)
    {
        omsSaleissuebillMapper.deleteOmsSaleissuebillentryByParentid(id);
        return omsSaleissuebillMapper.deleteOmsSaleissuebillById(id);
    }

    /**
     * 新增销售出库单明细信息
     * 
     * @param omsSaleissuebill 销售出库单对象
     */
    public void insertOmsSaleissuebillentry(OmsSaleissuebill omsSaleissuebill)
    {
        List<OmsSaleissuebillentry> omsSaleissuebillentryList = omsSaleissuebill.getOmsSaleissuebillentryList();
        Long id = omsSaleissuebill.getId();
        if (StringUtils.isNotNull(omsSaleissuebillentryList))
        {
            List<OmsSaleissuebillentry> list = new ArrayList<OmsSaleissuebillentry>();
            for (OmsSaleissuebillentry omsSaleissuebillentry : omsSaleissuebillentryList)
            {
                omsSaleissuebillentry.setParentid(id);
                list.add(omsSaleissuebillentry);
            }
            if (list.size() > 0)
            {
                omsSaleissuebillMapper.batchOmsSaleissuebillentry(list);
            }
        }
    }

    @Override
    public List<OmsSaleissuebill> selectSumBillNumberIsNull() {
        return omsSaleissuebillMapper.selectSumBillNumberIsNull();
    }

    /**
     * 根据id来设置汇总单编码数据
     * @param sumBillNumber 汇总单编码
     * @param ids id的集合
     * @return 结果
     */
    public int updateSumBillNumberByIds(String sumBillNumber, ArrayList<Long> ids){
        return omsSaleissuebillMapper.updateSumBillNumberByIds(sumBillNumber, ids);
    }

    /**
     * 查询是否推送过EAS的销售出库单数据
     * @param isTrans
     * @return
     */
    public List<OmsSaleissuebill> selectPushEASSaleissuebillList(int isTrans){
        return omsSaleissuebillMapper.selectPushEASSaleissuebillList(isTrans);
    }
}
